package com.example.administrator.textproject;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private RadioGroup radioGroup;


    RadioButton conversationBtn;
    RadioButton friendBtn;
    RadioButton dynamicBtn;
    RadioButton profileBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        radioGroup = (RadioGroup) findViewById(R.id.container_group);
        conversationBtn = (RadioButton)radioGroup. findViewById(R.id.conversation_btn);
        friendBtn = (RadioButton)radioGroup. findViewById(R.id.friend_btn);
        dynamicBtn = (RadioButton) radioGroup.findViewById(R.id.dynamic_btn);
        profileBtn = (RadioButton)radioGroup. findViewById(R.id.profile_btn);


        conversationBtn.setChecked(true);
        addFragment(new OneFragment(), "OneFragment");
        conversationBtn.setOnClickListener(this);
        friendBtn.setOnClickListener(this);
        dynamicBtn.setOnClickListener(this);
        profileBtn.setOnClickListener(this);
    }

    private void addFragment(Fragment fragment, String Tag) {

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        //先用Tag去内存中查找有没有这样的Fragment
        transaction.replace(R.id.home_fragment_container, fragment, Tag);
        transaction.commit();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.conversation_btn:
                addFragment(new OneFragment(), "OneFragment");
                break;
            case R.id.friend_btn:
                addFragment(new TwoFragment(), "TwoFragment");
                break;
            case R.id.dynamic_btn:
                addFragment(new ThreeFragment(), "ThreeFragment");
                break;
            case R.id.profile_btn:
                addFragment(new FourFragment(), "FourFragment");
                break;
            default:
                break;
        }

    }
}
